﻿Imports util
Imports System.Data.SqlServerCe

Public Class ConfiguracionService
    Private app As String = "app.sdf"
    Dim con

    Public Sub New()
        Dim existe As Boolean = System.IO.File.Exists(app)

        con = SqlCEUtil.getConnection(app, False, "aplicacion")

        Dim cmd As New SqlCeCommand()
        cmd.Connection = con
        Try
            con.Open()

            If Not existe Then
                cmd.CommandText = "CREATE TABLE configuracion(" & _
                                "id int NOT NULL, " & _
                                "codigo nvarchar(50) NOT NULL, " & _
                                "CONSTRAINT pk_Configuracion PRIMARY KEY (id)" & _
                                ");"
                cmd.ExecuteNonQuery()

                cmd.CommandText = "insert into configuracion(id,codigo) values (1,'')"
                cmd.ExecuteNonQuery()
            End If
            cmd.CommandText = "select * from configuracion where id=1"
            Dim dr As SqlCeDataReader = cmd.ExecuteReader()

        Catch ex As Exception
            MsgBox(ex.ToString)
        Finally
            con.Close()
        End Try
    End Sub

    Function getCodigo() As String
        
        Dim cmd As New SqlCeCommand()
        cmd.Connection = con

        Try
            con.Open()

            cmd.CommandText = "select * from configuracion where id=1"
            Dim dr As SqlCeDataReader = cmd.ExecuteReader()

            If dr.Read Then
                Return dr("codigo").ToString()
            End If

        Catch ex As Exception
            MsgBox(ex.ToString)
        Finally
            con.Close()
        End Try

        Return ""
    End Function

    Sub guardarCodigo(ByVal codigo As String)
        Dim cmd As New SqlCeCommand()
        cmd.Connection = con

        Try
            con.Open()

            cmd.CommandText = "update configuracion set codigo='" & codigo & "' where id=1"
            cmd.ExecuteNonQuery()

        Catch ex As Exception
            MsgBox(ex.ToString)
        Finally
            con.Close()
        End Try
    End Sub
End Class
